#!/usr/bin/env bash
# Copyright ©2017-2023 Jason Trunks
# https://notabug.org/JasKinasis/cmus-scripts

# Another slightly insane usage of nowplay
# Monitor cmus and send a message to ALL pty's 
# each time the players state changes

# TODO: Look into using something like mesg or wall to send messages to all ttys.
# wall writes a message to all ttys - to indentify the tty you are in you can use tty
# which usually yields ptyx - where x is a number
# else list /dev and grep for all pty's and then use a for loop to output messages...
# see: https://unix.stackexchange.com/questions/261531/how-to-send-output-from-one-terminal-to-another-without-making-any-new-pipe-or-f

# Temporarily commented out the code to get all pty's and send messages to all.
# For now it just displays the now playing track in cmus in whichever pty it was first ran in.
# It would be better to set up a subscriber/observer pattern
# where we could register the tty's that we want messages sent to

prev=""
#ptys="$(ls /dev | \grep pty)"

while true; do 
	now=$(nowplay);
  if [[ "$now" != "$prev" ]]; then
	prev=$now
	#for line in `echo -e "$ptys"`
    #do
	#	echo $now > /dev/$line;
	#done
	#ptyMsg "$now"
	#echo "$now"
	wall "$now"
  fi
  sleep 5;
done

